import React, { Component } from 'react'
import PropTypes from 'prop-types'
import './Header.css'

export default class Header extends Component {
  static propTypes = {
    addTodo: PropTypes.func.isRequired
  }


  // 向todos中加任务
  handleAdd = (event)=>{
    // 按回车才生效
    if (event.keyCode !== 13) return
    // 输入不为空
    if (event.target.value === '') return
    let {todos} = this.props
    const task = {
      id: todos.length+1,
      name: event.target.value,
      done: false
    }
    // 向父组件传递一条新的todo对象
    this.props.addTodo(task)
    // 清空输入框
    event.target.value = ''
  }
  

  render() {
    return (
      <div className="todo-header">
        <input type="text" onKeyUp={this.handleAdd} placeholder="请输入你的任务名称，按回车确认"/>
      </div>
    )
  }
}
